using System;
using System.Text;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Transactions;

using NBear.Common;
using NBear.Data;

namespace NBear.Test.UnitTests
{
    [Table("Categories")]
    public interface CategoryChild : Category
    {
    }

    /// <summary>
    /// Summary description for TestActiveEntity
    /// </summary>
    [TestClass]
    public class TestActiveEntity
    {
        private static Gateway gateway;

        public TestActiveEntity()
        {
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            gateway = new Gateway(DatabaseType.SqlServer, "Server=(local);Database=Northwind;Uid=sa;Pwd=sa");
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestActiveEntityUsage()
        {
            ActiveEntity<Category> obj2 = new ActiveEntity<Category>(gateway);
            Assert.AreEqual(obj2.Status, ActiveEntityStatus.New);

            ActiveEntity<Category> obj = new ActiveEntity<Category>(gateway, 1);
            Assert.AreEqual(obj.Status, ActiveEntityStatus.Loaded);

            string newCatName = Guid.NewGuid().ToString().Substring(0, 5);
            obj.Entity.CategoryName = newCatName;
            obj.Save();

            obj = new ActiveEntity<Category>(gateway, 1);
            Assert.AreEqual(newCatName, obj.Entity.CategoryName);

            obj2.Entity.CategoryName = obj.Entity.CategoryName + "_new";
            obj2.Entity.Description = obj.Entity.Description;
            obj2.Entity.Picture = obj.Entity.Picture;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            {
                obj2.Save();
                Assert.IsTrue(obj2.Entity.CategoryID > 0);

                obj2.Delete();
                Assert.AreEqual(obj2.Status, ActiveEntityStatus.New);

                scope.Complete();
            }

            ActiveEntity<CategoryChild> obj3 = new ActiveEntity<CategoryChild>(gateway);
            obj3.Entity.CategoryName = obj.Entity.CategoryName + "_new";
            obj3.Entity.Description = obj.Entity.Description;
            obj3.Entity.Picture = obj.Entity.Picture;
            obj3.Save();
            Assert.IsTrue(obj3.Entity.CategoryID > 0);

            DbTransaction tran = gateway.BeginTransaction();

            obj3.Entity.CategoryName = "teddy";
            obj3.Save(tran);

            obj = new ActiveEntity<Category>(gateway, 1);
            newCatName = Guid.NewGuid().ToString().Substring(0, 5);
            obj.Entity.CategoryName = newCatName;
            obj.Save(tran);

            tran.Commit();

            //obj3.Delete();

            gateway.CascadeDelete<CategoryChild>(obj3.Entity);
        }
    }
}
